# -*- coding: utf-8 -*-
import urllib2
import re

from foreignsites.parse.parsehtml import parsehtml, HtmlTag
from foreignsites.browser import Browser, BrowserError 

SITE = 'ru.wikipedia.org'
#SITE = 'test.tfolder.ru'
WIKI_PAGES = '/wiki/'
RESULT_DIR = 'ru_wikipedia/' 

def main():    
    page = 'А._А._Зализняк'    

#    browser = Browser()
#    browser.open('http://%s%s%s' % (SITE, WIKI_PAGES, page))    
#    browser.save('RESULT_DIR%s.html' % page)
            
    html = open('w.html').read().decode('utf-8')
    res = open('w.txt', 'w')    
    
    links = []    
    header = None
    #list(skiptagcontent(skip, parsehtml(html)))
    i = 0
    for elem, stat in parsehtml(html):
        openednames = [opened.name for opened in stat]
                
        for n in openednames:
            if n in ('script', 'style'): 
                continue
            
            if opened == '<div class="printfooter">':
                break
            
            if opened == '<h1 class="firstHeading">' and elem.is_text:
                header = elem.text
                continue

            if opened.name == 'a' and elem.is_text:
                href = opened.attrs.get('href')
                if href and href.startswith('/wiki/'):                        
                    links.append((elem.text, urllib2.unquote(href)))
                else:
                    continue
        
        if elem.is_text:            
            print elem.text.encode('utf-8')

        
    print ('HEADER: %s' % header).encode('utf-8')
    print ('LINKS: %s' % '\n'.join(('%s %s' % (k, v)) for k, v in links)).encode('utf-8')        
#        i += 1
#        if elem.is_text and elem.text.strip():
#            print '%04d: %s' % (i, elem.text.encode('utf-8'))
#        else:
#            print '%04d: %s' % (i, elem.__str__().encode('utf-8'))
#        #print 'xxxx: %s' % [elem.__str__().encode('utf-8') for elem in stat]
#        print 'xxxx: %s' % [elem.name for elem in stat]
        
        
#    for elem in skiptagcontent(skip, parsehtml(html)):
#        if elem.is_tag:
#            if elem == '<h1 class="firstHeading">':
#                isheader = True
#                continue
#            elif elem == '</h1>':
#                isheader = False
#                continue
#
#        if elem.is_text:
#            if isheader:
#                print 'HEADER: %s' % elem.text.encode('utf-8')
#            else:
#                print elem.text.encode('utf-8')

    res.close()
    
    #begin_text = '<h1 class="firstHeading">'
    #html = re.sub("\n|\r|\t", "", html)
    
#    html = re.sub(">\s+<", "><", html)
#    matches = re.search( self.archiveexp, html )
    
    #print html



main()